<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2021/3/29
  Time: 16:49
  To change this template use File | Settings | File Templates.
--%>
<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>

    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">

    <jsp:include page="../common/header.jsp"></jsp:include>
    <jsp:include page="../common/menu.jsp"></jsp:include>


    <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">
            <fieldset class="layui-elem-field layui-field-title" style="...">
                <legend>人员列表</legend>
            </fieldset>

            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">输入框</label>
                        <div class="layui-input-inline">
                            <input type="text" name="lastName" placeholder="请输入标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">开始时间</label>
                        <div class="layui-input-inline">
                            <input  name="start" type="text" class="layui-input" id="start">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">结束时间</label>
                        <div class="layui-input-inline">
                            <input  name="end" type="text" class="layui-input" id="end">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <button class="layui-btn" lay-submit lay-filter="formEmployee">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>
            </form>

            <div>
                <table class="layui-hide" id="test" lay-filter="test"></table>
                <script type="text/html" id="toolbarDemo">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
                        <%--                        <button class="layui-btn layui-btn-sm" lay-event="addEmployee">添加</button>--%>
                        <button class="layui-btn layui-btn-sm" lay-event="addEmp">添加</button>
                    </div>
                </script>
                <script type="text/html" id="barDemo">
                    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                </script>
            </div>
        </div>
    </div>


    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © layui.com - 底部固定区域s
    </div>
</div>

<div  style="padding: 20px ;display:none"  id="addEmployeeDiv">
    <%--        添加页面--%>
    <form class="layui-form" lay-filter="EmpForm" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">lastName</label>
            <div class="layui-input-block">
                <input type="text" name="lastName" placeholder="请输入lastName" autocomplete="off" class="layui-input">
                <input type="hidden" name="id">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">firstName</label>
            <div class="layui-input-block">
                <input type="text" name="firstName" placeholder="请输入firstName" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">address</label>
            <div class="layui-input-block">
                <input type="text" name="address" placeholder="请输入address" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-block">
                <input type="text" name="age" placeholder="age" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">薪资</label>
            <div class="layui-input-block">
                <input type="text" name="salary" placeholder="salary" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-input-block">
                <select id="deptList" name="deptId" lay-verify="required">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="gender" value="男" title="男" checked>
                <input type="radio" name="gender" value="女" title="女">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">时间</label>
            <div class="layui-input-block">
                <input type="text" name="date" id="date" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="addEmp">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>


<script src="${pageContext.request.contextPath}/layui/layui.js"></script>
<script>
    var $;
    layui.use(['form','table','laydate','jquery','layer'], function(){

        var form = layui.form;

        var table = layui.table;

        //时间空间
        var laydate=layui.laydate;

        $ = layui.$;

        var index = '';

        table.render({
            elem: '#test'
            ,url:'employeelist'
            ,method:"post"
            ,contentType:"application/json"
            ,toolbar: '#toolbarDemo'
            ,title: '用户数据表'
            ,totalRow: true
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
                ,{field:'lastName', title:'用户姓', width:80, edit: 'text'}
                ,{field:'firstName', title:'用户名', width:80, edit: 'text'}
                ,{field:'age', title:'年龄', width:60, edit: 'text'}
                ,{field:'address', title:'地址', width:100    , edit: 'text'}
                ,{field:'gender', title:'性别', width:60, edit: 'text', templet: function(res){
                        return res.gender == "男" ?"男" : "女" ;
                    }}
                ,{field:'salary', title:'工资', width:100, edit: 'text'}
                ,{field:'deptId', title:'部门编号', width:90, edit: 'text'}
                ,{field:'date', title:'入职时间', width:90, edit: 'text',hide:true }
                ,{field:'date2', title:'入职时间', width:120, edit: 'text'
                    ,templet:'<span>{{# var f=function(){var t=new Date(d.date);return t.getFullYear()+"-"+(t.getMonth()+1)+"-"+t.getDate()}}}{{f()}}</span>'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                ,groups: 5 //只显示 1 个连续页码
                ,first: false //不显示首页
                ,last: false //不显示尾页
                ,limits:[1,2,5,10,20] //设置页容量选项
                ,limit:5 // 设置默认页容量
            }
        });

        //工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                // case 'addEmployee':
                case 'addEmp':
                    //查询dept数据
                    getDeptlist();
                    //弹出框
                    index=layer.open({
                        type: 1,
                        title: ['添加人员', 'font-size: 18px;'],
                        area: ['700px', '660px'],
                        shadeClose: false , //点击遮罩关闭
                        content: $("#addEmployeeDiv")
                    });
                    break;

            };
        });
        //工具栏事件2
        table.on('tool(test)', function(obj){
            var data=obj.data;//获取当前行数据
            var layEvent=obj.event;//获取 lay-event 对应的值 （也可以是表头的event 参数和对应的值）
            var tr=obj.tr;
            if(layEvent==='del'){ //删除
                layer.confirm('真的删除吗',function (index){
                    //obj.del();//删除对应行（tr）的 DOM 结构 并更新缓存
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        url:'${pageContext.request.contextPath}/employee/del',
                        dataType:'text',
                        data:JSON.stringify(data.id),
                        contentType: 'application/json',
                        type: 'post',
                        success:function (data) {
                            if(data === 'true'){
                                layer.msg("删除成功");
                                layer.close(index);
                                //重载表格
                                table.reload('test');
                            }
                        }
                    })
                });
            }else if(layEvent==='edit'){
                //编辑
                index=layer.open({
                    type:1,
                    title:['修改人员','font-size:18px;'],
                    area:['800px','560px'],
                    shadeClose:false,//点击遮罩关闭
                    content: $("#addEmployeeDiv"),
                    success: function (layero,index) {
                        $.ajax({
                            url:'${pageContext.request.contextPath}/dept/getList',
                            dataType:'JSON',
                            type: 'get',
                            async: false,
                            success:function(d){
                                $("deptList").html("<option value='0'>请选择</option>");
                                $.each(d,function (index,item){
                                    var option=new Option(item.deptName,item.id);
                                    if (item.id == data.deptId){
                                        option.setAttribute("select","true");
                                    }
                                    $('#deptList').append(option);
                                })
                                layui.form.render("select");//重新渲染固定写法
                            }
                        })
                        form.val("EmpForm",{
                            "id":data.id,
                            "lastName":data.lastName,
                            "firstName":data.firstName,
                            "address":data.address,
                            "date":data.date,
                            "gender":data.gender,
                        });
                    }

                });
            }

        });

        //监听提交  更新
        form.on('submit(addEmp)',function (data){
            if (data.field.id != ''){
                $.ajax({
                    url:'${pageContext.request.contextPath}/employee/update',
                    dataType:'text',
                    data:JSON.stringify(data.field),
                    contentType: 'application/json',
                    type: 'post',
                    success:function (data) {
                        if(data === 'true'){
                            layer.msg("修改成功");
                            layer.close(index);
                            //重载表格
                            // table.reload('test');
                        }
                    }
                })
            }else{
                //监听提交  添加
                $.ajax({
                    url:'${pageContext.request.contextPath}/employee/insert',
                    dataType:'text',
                    data:JSON.stringify(data.field),
                    contentType: 'application/json',
                    type: 'post',
                    success:function (data) {
                        if(data === 'true'){
                            layer.msg("添加成功");
                            layer.close(index);
                            //重载表格
                            table.reload('test');
                        }
                    }
                })
            }
            return false;
        })


        laydate.render({
            elem: '#start' //指定元素
        });
        laydate.render({
            elem: '#end' //指定元素
        });
        laydate.render({
            elem: '#date' //指定元素
        });


        //监听提交
        form.on('submit(formEmployee)', function (data){
            table.reload('test',{
                where: {  //设定异步数据接口的额外参数，任意设置
                    lastName: data.field.lastName,
                    start: data.field.start,
                    end: data.field.end,
                }
            });
            return false;
        });
    });


    // $=layui.$;
    function getDeptlist(){
        //发送ajax
        $.ajax({
            url: '${pageContext.request.contextPath}/dept/getList',
            dataType: 'text',
            contentType: 'javascript/json',
            type: 'get',
            success: function (data) {
                var dept=JSON.parse(data)
                var str='<option value="0">请选择</option>';
                $.each(dept,function () {
                    str +='<option value="' + this.id + '">'+this.deptName+'</option>';
                })
                $("#deptList").html(str);
                layui.form.render("select");
            }
        });

    }
</script>

</body>

</html>

